package com.example.autopoi.controller;

import com.example.autopoi.entity.CompanyImgModel;
import com.example.autopoi.entity.PmsSupplierParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.def.TemplateExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecgframework.poi.excel.view.JeecgTemplateExcelView;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author : Bruce Lee
 * @CreateTime : 2024/6/6
 */

@RestController
public class TestController {


    @RequestMapping(value = "/exportTemplate", method = RequestMethod.GET)
    public ModelAndView exportTemplate(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView(new JeecgTemplateExcelView());
//        List exportListData = null;

        // 导出文件名称
        mv.addObject(TemplateExcelConstants.FILE_NAME, "供应商信息模板");
        // 注解对象Class
        mv.addObject(TemplateExcelConstants.CLASS, PmsSupplierParam.class);
        // 自定义表格参数
        mv.addObject(TemplateExcelConstants.PARAMS, new TemplateExportParams("供应商信息模板", "供应商信息模板"));
        // 导出数据列表
        mv.addObject(TemplateExcelConstants.LIST_DATA, null);
        return mv;
    }

    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
    public ModelAndView exportXls() {
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List exportListData = getListData();

        // 导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "供应商信息");
        // 注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, PmsSupplierParam.class);
        // 自定义表格参数
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("供应商信息", "供应商信息"));
        // 导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST, exportListData);
        return mv;
    }

    /**
     * 生成假数据，真实开发中这里数据一般来自数据库
     *
     * @return
     */
    @SuppressWarnings("unused")
    private List getListData() {
        List<PmsSupplierParam> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            PmsSupplierParam excelEntity = new PmsSupplierParam();
            excelEntity.setName("名称" + i);
            excelEntity.setContact("联系人" + i);
            excelEntity.setContactPhone("联系电话" + i);
            excelEntity.setAddress("地址" + i);
            excelEntity.setRemark("备注" + i);
            list.add(excelEntity);
        }
        System.out.println(list.toString());
        return list;
    }

    @PostMapping("/importXls")
    @ResponseBody
    public String importExcel(MultipartFile file, Principal principal)
            throws Exception {
        // 设置导入参数
        ImportParams params = new ImportParams();
        // 表格标题所在行，计数从0开始
        params.setTitleRows(1);
        // head头部所在行，计数从0开始
        params.setHeadRows(0);
        // 表格sheet数量
        // params.setSheetNum(9);
        //
        params.setNeedSave(true);
        List<PmsSupplierParam> list = ExcelImportUtil.importExcel(file.getInputStream(), PmsSupplierParam.class, params);

        return list.toString();
    }


    /**
     * 基本导入
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.GET)
    public void importExcel() {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);  // 表头占一行
        params.setHeadRows(1);   // 列名占一行

        List<CompanyImgModel> userList = ExcelImportUtil.importExcel(new File("C:\\\\Users\\\\fkjas\\\\Desktop\\\\user.xlsx"), CompanyImgModel.class, params);

        userList.forEach(System.out::println);
    }

    /**
     * 基本导出
     */
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    public void exportExcel() {
        List<CompanyImgModel> userList = new ArrayList<>();
        userList.add(new CompanyImgModel("imgs/baidu.jpg", "aaa", "zhangsan@test.com"));
        userList.add(new CompanyImgModel("imgs/baidu.jpg", "bbb", "lisi@test.com"));


        ExportParams exportParams = new ExportParams("用户信息", "用户");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CompanyImgModel.class, userList);

        try (FileOutputStream fos = new FileOutputStream("C:\\Users\\fkjas\\Desktop\\user.xlsx")) {
            workbook.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
